#include "Stdafx.h"
#include <stdlib.h>
#include <string>
#include "api.h"

using namespace System::Runtime::InteropServices;

STRINGCS UnmanagedString2ManagedString(TCHAR *pIn)
{
	STRINGCS strOut;
#ifdef UNICODE
	strOut = Marshal::PtrToStringUni(static_cast<System::IntPtr>(pIn));
#else
	strOut = Marshal::PtrToStringAnsi(static_cast<System::IntPtr>(pIn));
#endif
	return strOut;
}

TCHAR* ManagedString2UnmanagedString(STRINGCS str)
{
#ifdef UNICODE
	return (wchar_t*)Marshal::StringToHGlobalUni(str).ToPointer();
#else
	return (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
#endif
}

char* toChar(const TCHAR* chars)
{
#ifdef UNICODE
	size_t len=(wcslen(chars)+1)*2;
	size_t converted=0;
	char* cstr=new char[len];
	setlocale(LC_ALL, "zh-CN");
	wcstombs_s(&converted,cstr,len,chars,_TRUNCATE);
	return cstr;
#else
	return (char*)chars;
#endif
}

TCHAR* toWChar(char* cstr)
{
#ifdef UNICODE
	size_t len=strlen(cstr)+1;
	size_t convetered=0;
	wchar_t* wstr=new wchar_t[len];
	mbstowcs_s(&convetered,wstr,len,cstr,_TRUNCATE);
	return wstr;
#else
	return cstr;
#endif	
}